﻿using B.S.Data.MES.API.Applications.approve.Command;
using B.S.Domain.Node;
using B.S.Infratrctrue;
using B.S.Requst;
using MediatR;

namespace B.S.Data.MES.API.Applications.approve.CommandHandlers
{
    public class UpdateShenPiCommandHandlers : IRequestHandler<UpdateShenpiCommand, APIRequst<int>>
    {
        private readonly IBaseRepository<ShenPiinfo> shenRepository;
        private readonly IBaseRepository<Settinginfo> settingRepository;

        public UpdateShenPiCommandHandlers(IBaseRepository<ShenPiinfo> shenRepository,IBaseRepository<Settinginfo> settingRepository)
        {
            this.shenRepository = shenRepository;
            this.settingRepository = settingRepository;
        }

        public async Task<APIRequst<int>> Handle(UpdateShenpiCommand request, CancellationToken cancellationToken)
        {
            var su = shenRepository.GetAll().FirstOrDefault(x => x.ShenPiId == request.ShenPiId);
            su.PersonId=request.PersonId;
            su.PersonName = request.PersonName;
            su.CurrNodeNo=su.NodeNextNo;
            su.CurrNodeName=su.NodeNextName;
            var sa = settingRepository.GetAll().FirstOrDefault(x => x.LastNodeNo == su.NodeNextNo);
            if (su.CurrNodeNo != "A004"&&request.Num==1)
            {
                su.NetxPersonId = sa.UserId;
                su.NetxPersonName = sa.UserNickName;
                su.NodeNextNo = sa.NodeNo;
                su.NodeNextName = sa.NodeName;
                su.State = "通过";
            }
            else
            {
                if (su.CurrNodeNo == "A004")
                {
                    su.NetxPersonId = 0;
                    su.NetxPersonName = "";
                    su.NodeNextNo = "";
                    su.NodeNextName = "";
                    su.State = "已完成";
                }
                if (request.Num == 2)
                {
                    su.NetxPersonId = 0;
                    su.NetxPersonName = "";
                    su.NodeNextNo = "";
                    su.NodeNextName = "";
                    su.State = "被拒绝";
                }
               
            }
            su.Yjian = request.Yjian;
            await shenRepository.Update(su);
            return new APIRequst<int>
            {
                Code = RequstCode.成功,
                Msg=RequstCode.成功.ToString(),
            };
            
        }
    }
}
